
import java.util.ArrayList;

/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
/**
 *
 * @author Andrea
 */
public class Testing {//Must write test results to a text file for permanent logging

    public static void main(String[] args) {
        DatabaseHelper vDatabaseHelper = new DatabaseHelper();

        XMLDTO testXML = new XMLDTO();
        testXML.setCode(-1);
        testXML.setCol(-1);
        testXML.setRow(-1);
        testXML.setQueryID(-1);
        testXML.setTeam("TEST-TEAM");
        
        

        System.out.println("");
        XMLHandler vXMLHandler = new XMLHandler();
        XMLExtractorTest(vXMLHandler, testXML);

        teamTest(vDatabaseHelper, testXML);
        fortressTest(vDatabaseHelper, testXML);
        resourceTest(vDatabaseHelper, testXML);
        landClaimTest(vDatabaseHelper, testXML);
        // Algorithm tests
        algorithmTest(vDatabaseHelper, testXML);
        architectTest(vDatabaseHelper, testXML);



    }

    public static void teamTest(DatabaseHelper vDatabaseHelper, XMLDTO testXML) {
        String Query;
        System.out.println("Team table unit testing initiated...");

        // INSERT
        Query = "INSERT INTO teams (Team_Name, Resources) values ('TEST-TEAM','12345')";
        vDatabaseHelper.executeSQLQuery(Query);
        Query = "INSERT INTO teams (Team_Name, Resources) values ('TEST-TEAM2','67890')";
        vDatabaseHelper.executeSQLQuery(Query);

        // SELECT
        ArrayList vArrayList = new ArrayList();
        vArrayList = vDatabaseHelper.returnTeamDTO(testXML);

        if (vArrayList.size() == 1) {
            System.out.println("INSERT statement: SUCCESSFUL");
            System.out.println("SELECT statement: SUCCESSFUL");
            System.out.println("DTO setter methods: SUCCESSFUL");
        } else {
            System.out.println("INSERT statement: FAILED");
            System.out.println("SELECT statement: FAILED");
            System.out.println("DTO setter methods: FAILED");
        }

        final int testInt = 12345;

        if (((((TeamDTO) vArrayList.get(0)).getTeamName()).equals(testXML.getTeam())) && ((((TeamDTO) vArrayList.get(0)).getResources()) == testInt)) {
            System.out.println("DTO getter methods: SUCCESSFUL");
        } else {
            System.out.println("DTO getter methods: FAILED");
        }

        // UPDATE
        Query = "UPDATE teams SET Team_Name='TEST-TEAM' WHERE Team_Name='TEST-TEAM2'";
        vDatabaseHelper.executeSQLQuery(Query);
        Query = "SELECT * FROM teams WHERE Resources = '12345'";
        vArrayList.clear();
        vArrayList = vDatabaseHelper.returnTeamDTO(testXML);

        if (vArrayList.size() == 2) {
            System.out.println("UPDATE statement: SUCCESSFUL");
        } else {
            System.out.println("UPDATE statement: FAILED");
        }

        // DELETE
        Query = "DELETE FROM teams WHERE Team_Name = 'TEST-TEAM'";
        vDatabaseHelper.executeSQLQuery(Query);
        Query = "SELECT * FROM teams WHERE Team_Name = 'TEST-TEAM'";
        vArrayList.clear();
        vArrayList = vDatabaseHelper.returnTeamDTO(testXML);

        if (vArrayList.isEmpty()) {
            System.out.println("DELETE statement: SUCCESSFUL");
        } else {
            System.out.println("DELETE statement: FAILED");
        }

        System.out.println("Team table unit testing completed.");
        System.out.println("");


    }

    public static void resourceTest(DatabaseHelper vDatabaseHelper, XMLDTO testXML) {

        String Query;
        System.out.println("Resource table unit testing initiated...");
        System.out.println("");

        // INSERT
        Query = "INSERT INTO resources (Grid_Row, Grid_Col, HasResources, Amount)";
        Query = Query + " values ('-1', '-1'," + true + ", '15000')";
        vDatabaseHelper.executeSQLQuery(Query);

        // SELECT
        ArrayList vArrayList = new ArrayList();
        vArrayList = vDatabaseHelper.returnResourceDTO(testXML);

        if (vArrayList.size() == 1) {
            System.out.println("INSERT statement: SUCCESSFUL");
            System.out.println("SELECT statement: SUCCESSFUL");
            System.out.println("DTO setter methods: SUCCESSFUL");
        } else {
            System.out.println("INSERT statement: FAILED");
            System.out.println("SELECT statement: FAILED");
            System.out.println("DTO setter methods: FAILED");
        }

        final int testInt = 15000;

        if (((((ResourceDTO) vArrayList.get(0)).isHasResources()))
                && ((((ResourceDTO) vArrayList.get(0)).getAmount() == testInt))
                && ((((ResourceDTO) vArrayList.get(0)).getGridColumn() == -1))
                && ((((ResourceDTO) vArrayList.get(0)).getGridRow() == -1))) {
            System.out.println("DTO getter methods: SUCCESSFUL");
        } else {
            System.out.println("DTO getter methods: FAILED");
        }

        // UPDATE
        Query = "UPDATE resources SET Grid_Row='-2' WHERE Grid_Row='-1'";
        vDatabaseHelper.executeSQLQuery(Query);
        vArrayList.clear();
        vArrayList = vDatabaseHelper.returnResourceDTO(testXML);

        if (vArrayList.isEmpty()) {
            System.out.println("UPDATE statement: SUCCESSFUL");
        } else {
            System.out.println("UPDATE statement: SUCCESSFUL");
        }

        // DELETE
        Query = "DELETE FROM resources WHERE amount = '15000'";
        vDatabaseHelper.executeSQLQuery(Query);
        vArrayList.clear();
        vArrayList = vDatabaseHelper.returnResourceDTO(testXML);

        if (vArrayList.isEmpty()) {
            System.out.println("DELETE statement: SUCCESSFUL");
        } else {
            System.out.println("DELETE statement: FAILED");
        }

        System.out.println("Resource table unit testing completed.");
        System.out.println("");
    }

    public static void fortressTest(DatabaseHelper vDatabaseHelper, XMLDTO testXML) {

        String Query;
        System.out.println("Fortress table unit testing initiated...");

        // INSERT
        Query = "INSERT INTO fortress (Fortress_Name, Strength_Level) values ('-1', '-100')";
        vDatabaseHelper.executeSQLQuery(Query);

        // SELECT
        ArrayList vArrayList = new ArrayList();
        vArrayList = vDatabaseHelper.returnFortressDTO(-1);

        if (vArrayList.size() == 1) {
            System.out.println("INSERT statement: SUCCESSFUL");
            System.out.println("SELECT statement: SUCCESSFUL");
            System.out.println("DTO setter methods: SUCCESSFUL");
        } else {
            System.out.println("INSERT statement: FAILED");
            System.out.println("SELECT statement: FAILED");
            System.out.println("DTO setter methods: FAILED");
        }

        final int testInt1 = -100;
        final int testInt2 = -1;
        if ((((FortressDTO) vArrayList.get(0)).getStrengthLevel() == testInt1)
                && (((FortressDTO) vArrayList.get(0)).getFortressName() == testInt2)) {
            System.out.println("DTO getter methods: SUCCESSFUL");
        } else {
            System.out.println("DTO getter methods: FAILED");
        }

        // UPDATE
        Query = "UPDATE fortress SET Strength_Level='-200' WHERE Fortress_Name='-1'";
        vDatabaseHelper.executeSQLQuery(Query);
        vArrayList.clear();
        vArrayList = vDatabaseHelper.returnFortressDTO(-1);

        if (vArrayList.size() == 1) {
            System.out.println("UPDATE statement: SUCCESSFUL");
        } else {
            System.out.println("UPDATE statement: SUCCESSFUL");
        }

        // DELETE
        Query = "DELETE FROM fortress WHERE Strength_Level = '-200'";
        vDatabaseHelper.executeSQLQuery(Query);
        vArrayList.clear();
        vArrayList = vDatabaseHelper.returnFortressDTO(-1);

        if (vArrayList.isEmpty()) {
            System.out.println("DELETE statement: SUCCESSFUL");
        } else {
            System.out.println("DELETE statement: FAILED");
        }

        System.out.println("Fortress table unit testing completed.");
        System.out.println("");

    }

    public static void landClaimTest(DatabaseHelper vDatabaseHelper, XMLDTO testXML) {

        String Query;
        System.out.println("LandClaim table unit testing initiated...");
        System.out.println("");

        // INSERT
        Query = "INSERT INTO land_claim (Grid_Row, Grid_Col, Fortress_Name, Team_Name, ";
        Query = Query + "IsDestroyed, Red_Active, Blue_Active) values ";
        Query = Query + " ('-1', '-1', '-50', 'TEST-TEAM', " + true + ", " + true + ", " + true + ")";
        vDatabaseHelper.executeSQLQuery(Query);

        // SELECT
        ArrayList vArrayList = new ArrayList();
        vArrayList = vDatabaseHelper.returnLandClaimDTO(testXML);

        if (vArrayList.size() == 1) {
            System.out.println("INSERT statement: SUCCESSFUL");
            System.out.println("SELECT statement: SUCCESSFUL");
            System.out.println("DTO setter methods: SUCCESSFUL");
        } else {
            System.out.println("INSERT statement: FAILED");
            System.out.println("SELECT statement: FAILED");
            System.out.println("DTO setter methods: FAILED");
        }

        if (((((LandClaimDTO) vArrayList.get(0)).isIsDestroyed()))
                && ((((LandClaimDTO) vArrayList.get(0)).isBlueActive()))
                && ((((LandClaimDTO) vArrayList.get(0)).isRedActive()))
                && ((((LandClaimDTO) vArrayList.get(0)).getFortressName() == -50))
                && ((((LandClaimDTO) vArrayList.get(0)).getTeamName().equals("TEST-TEAM")))
                && ((((LandClaimDTO) vArrayList.get(0)).getGridColumn() == -1))
                && ((((LandClaimDTO) vArrayList.get(0)).getGridRow() == -1))) {
            System.out.println("DTO getter methods: SUCCESSFUL");
        } else {
            System.out.println("DTO getter methods: FAILED");
        }

        // UPDATE
        Query = "UPDATE land_claim SET Grid_Col='-2' WHERE Grid_Col='-1'";
        vDatabaseHelper.executeSQLQuery(Query);
        vArrayList.clear();
        vArrayList = vDatabaseHelper.returnLandClaimDTO(testXML);

        if (vArrayList.size() == 0) {
            System.out.println("UPDATE statement: SUCCESSFUL");
        } else {
            System.out.println("UPDATE statement: SUCCESSFUL");
        }

        // DELETE
        Query = "DELETE FROM land_claim WHERE Fortress_Name = '-50'";
        vDatabaseHelper.executeSQLQuery(Query);
        vArrayList.clear();
        vArrayList = vDatabaseHelper.returnLandClaimDTO(testXML);

        if (vArrayList.isEmpty()) {
            System.out.println("DELETE statement: SUCCESSFUL");
        } else {
            System.out.println("DELETE statement: FAILED");
        }

        System.out.println("LandClaim table unit testing completed.");
        System.out.println("");
    }

    public static void XMLExtractorTest(XMLHandler vXMLHandler, XMLDTO testXML) {
        System.out.println("XML extraction unit testing initiated...");
        final String teamtest = "TEST-TEAM";
        final int row_test = 60932;
        final int col_test = 60810;
        final double lat_test = -26.1585943;
        final double long_test = 27.9902938;
        
        if ((testXML.getCode() == -1) && (testXML.getRow() == -1) && (testXML.getCol() == -1)
                && (testXML.getQueryID() == -1) && ((testXML.getTeam()).equals(teamtest))) {
            System.out.println("DTO getter methods: SUCCESSFUL");
            System.out.println("DTO setter methods: SUCCESSFUL");
        } else {
            System.out.println("DTO getter methods: FAILED");
            System.out.println("DTO setter methods: FAILED");
        }

        // include test for conversion of latitude to longitude
        XMLDTO QueryObject = vXMLHandler.extract_xml("src//query0.xml"); // Test XML file, necessary to do all fields?
        int code = QueryObject.getCode();
        // this if statement will change to include all the get methods - if their results are as expected then the get and set methdos work
        if (code == 1) {
            System.out.println("XML code extraction: SUCCESSFUL");
        } else {
            System.out.println("XML code extraction: FAILED");
        }
        
        
        if (QueryObject.getRow() == row_test)
        {
            System.out.println("XML row conversion: SUCCESSFUL");
        }
        else
        {
            System.out.println("XML row conversion: FAILED");
        }
        
        if (QueryObject.getCol() == col_test)
        {
            System.out.println("XML column conversion: SUCCESSFUL");
        }
        else
        {
            System.out.println("XML column conversion: FAILED");
        }

        System.out.println("XML extraction unit testing completed.");
        System.out.println("");

    }

    public static void architectTest(DatabaseHelper vDatabaseHelper, XMLDTO testXML) {
        System.out.println("Architect Interface unit testing initiated...");
        String vQuery = null;
        vQuery = "INSERT INTO resources (Grid_Row, Grid_Col, HasResources, Amount)";
        vQuery = vQuery + " values ('-1', '-1'," + true + ", '15000')";
        vDatabaseHelper.executeSQLQuery(vQuery);

        System.out.println("Architect Interface boolean unit testing initiated...");
        Architect tempArchitect = new Architect();
        if (tempArchitect.isNotResource(testXML, vDatabaseHelper) == false) {
            System.out.println("Check for resource method: SUCCESSFUL");
        } else {
            System.out.println("Check for resource method: FAILED");
        }

        vQuery = "DELETE FROM resources WHERE amount = '15000'";
        vDatabaseHelper.executeSQLQuery(vQuery);

        vQuery = "INSERT INTO land_claim (Grid_Row, Grid_Col, Fortress_Name, Team_Name, ";
        vQuery = vQuery + "IsDestroyed, Red_Active, Blue_Active) values ";
        vQuery = vQuery + " ('-1', '-1', '-50', 'TEST-TEAM', " + true + ", " + true + ", " + true + ")";
        vDatabaseHelper.executeSQLQuery(vQuery);

        if (tempArchitect.didNotExist(testXML, vDatabaseHelper) == false) {
            System.out.println("Check if block exists method: SUCCESSFUL");
            if (tempArchitect.isNotOwned(testXML, vDatabaseHelper) == false) {
                System.out.println("Check if block is owned method: SUCCESSFUL");
            } else {
                System.out.println("Check if block is owned method: FAILED");
            }
        } else {
            System.out.println("Check if block exists method: FAILED");
            System.out.println("Check if block is owned method: FAILED");
        }

        vQuery = "DELETE FROM land_claim WHERE Team_Name = 'TEST-TEAM'";
        vDatabaseHelper.executeSQLQuery(vQuery);

        System.out.println("Architect Interface database methods unit testing initiated...");

        testXML.setTeam("Red");
        vDatabaseHelper.activate_block(testXML);
        ArrayList vArrayList = new ArrayList();
        vArrayList = vDatabaseHelper.returnLandClaimDTO(testXML);
        if ((vArrayList.size() == 1)
                && (((LandClaimDTO) vArrayList.get(0)).isRedActive())) {
            System.out.println("Activate new block: SUCCESSFUL");
        } else {
            System.out.println("Activate new block: FAILED");
        }

        testXML.setTeam("Blue");
        vArrayList.clear();
        vDatabaseHelper.reactivate_block(testXML);
        vArrayList = vDatabaseHelper.returnLandClaimDTO(testXML);

        if ((vArrayList.size() == 1)
                && (((LandClaimDTO) vArrayList.get(0)).isBlueActive())) {
            System.out.println("Reactivate old block: SUCCESSFUL");
        } else {
            System.out.println("Reactivate old block: FAILED");
        }

        vQuery = "DELETE FROM land_claim WHERE Grid_Row = '-1'";
        vDatabaseHelper.executeSQLQuery(vQuery);
        System.out.println("Architect Interface unit testing completed.");
        System.out.println("");
    }
    
    public static void algorithmTest(DatabaseHelper vDatabaseHelper, XMLDTO testXML){
        System.out.println("Algorithm unit testing initiated...");
        
        Algorithms testAlgorithm = new Algorithms();
        if ((testAlgorithm.getRandomResult(1)) && (testAlgorithm.getRandomResult(0) == false)){
            System.out.println("Random Number Generator: SUCCESSFUL");
        }else{
            System.out.println("Random Number Generator: FAILED");
        }
        
        //----------------------------------------------------------------------
        //reactivte resource
        String vQuery = null;
        vQuery = "INSERT INTO resources (Grid_Row, Grid_Col, HasResources, Amount)";
        vQuery = vQuery + " values ('-1', '-1'," + false + ", '15000')";
        vDatabaseHelper.executeSQLQuery(vQuery);
        vDatabaseHelper.reactivate_resource(-1, -1);
        ArrayList vArrayList = new ArrayList();
        vArrayList = vDatabaseHelper.returnResourceDTO(testXML);
        
        if (vArrayList.size() == 1){
            if ((((ResourceDTO) vArrayList.get(0)).isHasResources())){
                System.out.println("Reactivate Resource: SUCCESSFUL");
            }else{
                System.out.println("Reactivate Resource: FAILED");
            }
        }else{
            System.out.println("Reactivate Resource: FAILED");
        }
        
        //----------------------------------------------------------------------
        vQuery = "DELETE FROM resources WHERE amount = '15000'";
        vDatabaseHelper.executeSQLQuery(vQuery);
        
        System.out.println("Algorithm unit testing completed.");
        System.out.println("");
    }
public static void scoutCaptureResourceTest(DatabaseHelper vDatabaseHelper, XMLDTO testXML) {
        System.out.println("Scout Interface unit testing initiated...");
        String vQuery = null;
        vQuery = "INSERT INTO land_claim (Grid_Row, Grid_Col, Red_Active, Blue_Active)";
        vQuery = vQuery + " values ('-1', '-1'," + true + ", false)";
        vDatabaseHelper.executeSQLQuery(vQuery);
        testXML.setTeam("Red");
     
        

        System.out.println("Scout Interface boolean unit testing initiated...");
        Scout tempScout = new Scout();
        if (tempScout.isActive(testXML, vDatabaseHelper) == true) {
            System.out.println("Check for Red active method: SUCCESSFUL");
        } else {
            System.out.println("Check for Red active method: FAILED");
        }
        
        testXML.setTeam("Blue");
        if (tempScout.isActive(testXML, vDatabaseHelper) == false) {
            System.out.println("Check for Blue active, when Red is actually active method: SUCCESSFUL");
        } else {
            System.out.println("Check for Blue active, when Red is actually active method: FAILED");
        }
        
        testXML.setTeam("Red");
        vQuery = "INSERT INTO resources (Grid_Row, Grid_Col, HasResources, Amount)";
        vQuery = vQuery + " values ('-1', '-1'," + true + ", 1000)";   
        vDatabaseHelper.executeSQLQuery(vQuery);
        
        vQuery = "INSERT INTO teams (Team_Name, Resources)";     //people won't ever get 1001 in game
        vQuery = vQuery + " values ('Red', 2001)";
        vDatabaseHelper.executeSQLQuery(vQuery);
        
        ArrayList teamList = vDatabaseHelper.returnTeamDTO(testXML);
        int current_resources = ((TeamDTO) teamList.get(0)).getResources();
        
        if (current_resources == 2001)
        {
            System.out.println("Get the team's resources: SUCCESSFUL");
        }
        else
        {
            System.out.println("Get the team's resources: FAILED");
        }
        
        ArrayList resourceList = vDatabaseHelper.returnResourceDTO(testXML);
        int captured_resources = ((ResourceDTO) resourceList.get(0)).getAmount();
        
         if (captured_resources == 1000)
        {
            System.out.println("Get the captured resources: SUCCESSFUL");
        }
        else
        {
            System.out.println("Get the captured resources: FAILED");
        }
         
        current_resources = current_resources + captured_resources;
        
        vDatabaseHelper.change_resource(((TeamDTO) teamList.get(0)).getTeamName(), current_resources);
        teamList = vDatabaseHelper.returnTeamDTO(testXML);
        int team_resources = ((TeamDTO) teamList.get(0)).getResources();
        
        if (team_resources == 3001)
        {
            System.out.println("Update the team resources: SUCCESSFUL");
        }
        else
        {
            System.out.println("Update the team resources: FAILED");
        }
        
        vDatabaseHelper.deactivate_resource(testXML.getRow(), testXML.getCol());
        
        vQuery = "DELETE FROM resources WHERE Grid_Row = '-1'";
        vDatabaseHelper.executeSQLQuery(vQuery);
        vQuery = "DELETE FROM teams WHERE Resources = '3001'";
        vDatabaseHelper.executeSQLQuery(vQuery);
        vQuery = "DELETE FROM land_claim WHERE Grid_Row = '-1'";
        vDatabaseHelper.executeSQLQuery(vQuery);
    }
        
        public static void isOwnedTest(DatabaseHelper vDatabaseHelper, XMLDTO testXML){
        //check that the block being captured is activated by the right team
        String vQuery = null;
        vQuery = "INSERT INTO land_claim (Grid_Row, Grid_Col, Red_Active, Blue_Active, Team_Name)";
        vQuery = vQuery + " values ('-1', '-1'," + true + ", false, 'Red')";
        vDatabaseHelper.executeSQLQuery(vQuery);
        
        testXML.setTeam("Blue");
        ArrayList landList = vDatabaseHelper.returnLandClaimDTO(testXML);
        
        Scout vScout = new Scout();
        
        if (vScout.isOwned(testXML, vDatabaseHelper) == false)
        {
            System.out.println("Check if block is owned by scout's team: SUCCESSFUL");
        }
        else
        {
            System.out.println("Check if block is owned by scout's team: FAILED");
        }
        
        vQuery = "DELETE FROM land_claim WHERE Grid_Row = '-1'";
        vDatabaseHelper.executeSQLQuery(vQuery);
    }    
        
        public static void destroyBlockTest(DatabaseHelper vDatabaseHelper,  XMLDTO testXML) {
        //update DB and overlay
        String vQuery = null;
        vQuery = "INSERT INTO land_claim (Grid_Row, Grid_Col, Red_Active, Blue_Active, Team_Name, IsDestroyed)";
        vQuery = vQuery + " values ('-1', '-1'," + true + ", false, 'Red', "+ false + ")";
        testXML.setTeam("Red");
        vDatabaseHelper.executeSQLQuery(vQuery);
        
        String team_name = "Blue"; 
        int team_resources = 2001;
        
        Scout vScout = new Scout();
        vScout.destroyBlock(testXML, team_name, team_resources);
        
        ArrayList vArrayList = vDatabaseHelper.returnLandClaimDTO(testXML);
        
        if ((((LandClaimDTO) vArrayList.get(0)).isIsDestroyed()))
        {
            System.out.println("Block Destroyed: SUCCESSFUL");
        }
        else
        {
            System.out.println("Block Destroyed: FAILED");
        }
        
        vQuery = "DELETE FROM land_claim WHERE Grid_Row = '-1'";
        vDatabaseHelper.executeSQLQuery(vQuery); 
    }
        
        public static void captureBlockTest(DatabaseHelper vDatabaseHelper,  XMLDTO testXML) {
        //update DB and overlay
        String vQuery = null;
        vQuery = "INSERT INTO land_claim (Grid_Row, Grid_Col, Red_Active, Blue_Active, Team_Name, IsDestroyed)";
        vQuery = vQuery + " values ('-1', '-1'," + true + ", false, 'Red', "+ false + ")";
        testXML.setTeam("Red");
        vDatabaseHelper.executeSQLQuery(vQuery);
        
        String team_name = "Blue"; 
        int team_resources = 2001;
        
        Scout vScout = new Scout();
        vScout.captureBlock(testXML, team_name, team_resources);
        
        ArrayList vArrayList = vDatabaseHelper.returnLandClaimDTO(testXML);
        
        if ((((LandClaimDTO) vArrayList.get(0)).getTeamName().equals("Blue")))
        {
            System.out.println("Block Captured: SUCCESSFUL");
        }
        else
        {
            System.out.println("Block Captured: FAILED");
        }
        
        vQuery = "DELETE FROM land_claim WHERE Grid_Row = '-1'";
        vDatabaseHelper.executeSQLQuery(vQuery); 
    }
}
